var crop_handlerPolygon = null;

function CropOnload() {
    crop_handlerPolygon = 1;
    //viewer.scene.globe.depthTestAgainstTerrain = true;
    $('#Crop_polygon').click(function() {
        Crop_clear();
        tracker.trackPoint(function(position) {
            var cartographic = Cesium.Cartographic.fromCartesian(position);
            var longitude = Cesium.Math.toDegrees(cartographic.longitude);
            var latitude = Cesium.Math.toDegrees(cartographic.latitude);
            var height = parseInt(cartographic.height);
            var cartesian3 = Cesium.Cartesian3.fromDegrees(longitude, latitude, 0);
            var modelMatrix;
            var clippingPlanes = loadClip(modelMatrix);
            for (var attr in cesium.layer3DList) {
                if (cesium.layer3DList[attr].isCesium3DTileset) {
                    //modelMatrix = cesium.layer3DList[attr].modelMatrix;
                    //clippingPlanes = loadClip(modelMatrix);
                    cesium.layer3DList[attr].clippingPlanes = clippingPlanes;
                }
            }
            for (var i = 0; i < clippingPlanes.length; i++) {
                var plane = clippingPlanes.get(i);
                viewer.entities.add({ //添加平面实体 直观裁切面
                    position: cartesian3, // offset, 根据3dtiles同步调整裁切面高度 
                    plane: {
                        dimensions: new Cesium.Cartesian2(100, 100), //多边形大小
                        material: Cesium.Color.BLUE.withAlpha(0.3), //多边形大小
                        plane: new Cesium.CallbackProperty(createPlaneUpdateFunction(plane), false),
                        //plane: plane,
                        outline: true,
                        outlineColor: Cesium.Color.BLUE
                    }
                });
            }
        });
    })

    $("#Crop_clear").click(function() {
        Crop_clear();
    })

    function loadClip(modelMatrix) {
        var clippingPlanes = new Cesium.ClippingPlaneCollection({
            planes: [
                new Cesium.ClippingPlane(new Cesium.Cartesian3(1.0, 0.0, 0.0), 0.0),
            ],
            edgeColor: Cesium.Color.RED.withAlpha(0.1), //模型的边缘颜色
            edgeWidth: 1.0, //模型的边缘宽度
            unionClippingRegions: false, //如果为true，则如果一个区域位于集合中任何平面的外部，则将对其进行裁剪。否则，仅当区域位于每个平面的外部时，区域才会被裁剪。
            modelMatrix: modelMatrix
        });
        return clippingPlanes;
    }

    function createPlaneUpdateFunction(plane) {
        return function() {
            if (window.localStorage.getItem('range_crop_distance')) {
                plane.distance = parseInt(window.localStorage.getItem('range_crop_distance'));
            } else {
                plane.distance = 0;
            }
            if (window.localStorage.getItem('crop_type')) {
                switch (window.localStorage.getItem('crop_type')) {
                    case "1": //水平
                        plane.normal.x = -1.0;
                        plane.normal.y = 0.0;
                        plane.normal.z = 0.0;
                        //plane.distance = plane.distance + 870;
                        break;
                    case "2": //垂直
                        plane.normal.x = 0.0;
                        plane.normal.y = 0.0;
                        plane.normal.z = -1.0;
                        break;
                }
            }
            return plane;
        }
    }
}

function Crop_clear(clear) {
    tracker && tracker.clear(); //清除绘制事件
    for (var attr in cesium.layer3DList) {
        if (cesium.layer3DList[attr] && cesium.layer3DList[attr].isCesium3DTileset && cesium.layer3DList[attr].clippingPlanes) {
            cesium.layer3DList[attr].clippingPlanes.enabled = false;
        }
    }
    if (!clear) { removeAllEntities(); }
}